package org.cable.account.controller;


import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.cable.account.controller.request.TrafficRequest;
import org.cable.account.controller.request.UseTrafficRequest;
import org.cable.account.service.TrafficService;
import org.cable.account.vo.TrafficVO;
import org.cable.common.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hsj
 * @since 2023-10-25
 */
@RestController
@RequestMapping("/api/traffic/v1")
public class TrafficController {

     @Autowired
	 private TrafficService trafficService;

	 @Value("${rpc.token}")
	 private String rpcToken;

	/**
	 * 分页查询可用得流量包
	 * @param request
	 * @return
	 */
	@RequestMapping("/page")
	 public JsonData pageAvailable(@RequestBody TrafficRequest request){
		Map<String,Object> map =  trafficService.pageTraffic(request);
		return JsonData.buildSuccess(map);
	 }



	/**
	 * 查找某个流量包详情
	 * @param trafficId
	 * @return
	 */
	@GetMapping("/detail/{trafficId}")
	public JsonData detail(@PathVariable("trafficId") long trafficId){

		TrafficVO trafficVO = trafficService.detail(trafficId);

		return JsonData.buildSuccess(trafficVO);
	}


	/**
	 * 使用流量包API
	 *
	 * @param useTrafficRequest
	 * @return
	 */
	@PostMapping("reduce")
	public JsonData useTraffic(@RequestBody UseTrafficRequest useTrafficRequest,HttpServletRequest request){

		String requestToken = request.getHeader("rpc-token");
		if (rpcToken.equalsIgnoreCase(requestToken)){
			JsonData jsonData = trafficService.reduce(useTrafficRequest);
			return jsonData;
		}else {
			return JsonData.buildError("非法访问");
		}
	}




}

